package model;

import java.util.*;
import controller.UserManager;
import dao.mysql.DAOFactoryImpl;

public class User {

	private String userID;
	private String userName;
	private String password;
	private String empID;
	private ArrayList roleIDs = new ArrayList();

	public User() {
		super();
	}

	public User(String userID, String userName, String password, String empID) {
		super();
		this.userID = userID;
		this.userName = userName;
		this.password = password;
		this.empID = empID;
	}

	public String getUserID() {
		return userID;
	}

	public void setUserID(String userID) {
		this.userID = userID;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getEmpID() {
		return empID;
	}

	public void setEmpID(String empID) {
		this.empID = empID;
	}

	public Role[] getRoles (){
		int numRoles = roleIDs.size();
		if (numRoles != 0){
			Role[] result= new Role[numRoles];
			Vector tempResult = new Vector();
			for(int i=0; i < numRoles; i++){
				try{
					String s = roleIDs.get(i).toString();
					UserManager userMgr = new UserManager();
					Role r= userMgr.findRole(s);
					tempResult.add(r);
				}catch (Exception e) {
	                System.out.println ("Error occour when getting list of role IDs: "+e);
				}
				result = (Role[]) tempResult.toArray (result);
		        return result;
			}
		}
		return null;
	}

	public void setRoles(Role newRoles[]) {
		clearRoleIDs ();
        for (int i = 0; i < newRoles.length; i++) {
            addRoleID (newRoles[i].getRoleID());
        }
	}

	public boolean hasRole(String roleID) {
		if (roleID != null) {
			Iterator i = roleIDs.iterator();
			while (i.hasNext()) {
				String role = (String) i.next();
				if (roleID.equals(role))
					return true;
			}
		}
		return false;
	}

	public void clearRoleIDs() {
		roleIDs = new ArrayList();
	}

	public void addRoleID(String newRoleID) {
		roleIDs.add(newRoleID);
	}

	public void addRole(Role newRole) {
		roleIDs.add(newRole.getRoleID());
	}
}